package com.blast.manager.delay.ui

import android.content.Context
import android.content.Intent
import android.view.KeyEvent
import androidx.recyclerview.widget.LinearLayoutManager
import com.blast.manager.R
import com.blast.manager.delay.adapter.UnauthorizedAdapter
import com.blast.manager.delay.presenter.ShowDelayDataPresenter
import com.wxl.common.AppContext
import com.wxl.common.base.AbsActivity
import com.wxl.common.life.ViewModelQuick
import com.wxl.common.room.entity.LeiGuanEntity
import com.wxl.common.room.entity.LogEntity
import com.wxl.common.util.LayoutHelper
import com.wxl.common.util.ObservableUtil
import kotlinx.android.synthetic.main.activity_unauthorized_layout.*

class UnauthorizedActivity : AbsActivity(){

    companion object {
        fun start(context: Context , data:ArrayList<LeiGuanEntity>){
            val intent = Intent(context, UnauthorizedActivity::class.java)
            AppContext.appContext.setLeiGuanEntity("unauthorizedData",data)
            intent.putExtra("key", "unauthorizedData")
            context.startActivity(intent)
        }
    }

    private lateinit var mNoAuthorizeList: ArrayList<LeiGuanEntity>

    private lateinit var mUnauthorizedAdapter: UnauthorizedAdapter

    override fun getCreateContentViewId(): Int {
        return R.layout.activity_unauthorized_layout
    }

    override fun onCreateViewChanged() {
        isCheckAppUpgrade = false
        setTopTitle("未授权页")
        val key = intent.getStringExtra("key")
        mNoAuthorizeList = AppContext.appContext.getLeiGuanEntity(key!!)
        unAuthListView.layoutManager = LinearLayoutManager(this)
        mUnauthorizedAdapter = UnauthorizedAdapter(mNoAuthorizeList)
        LayoutHelper.getDetonatorTitleViewsWidth(unauthorizedTitleLayout,object : ObservableUtil.Companion.OnNextCallback<ArrayList<Int>>(){
            override fun onNext(t: ArrayList<Int>) {
                mUnauthorizedAdapter.setWidths(t)
                mUnauthorizedAdapter.bindToRecyclerView(unAuthListView)
            }
        })

        unAuthCancel.setOnClickListener {
            addLog("返回","点击返回")
            finish()
        }
        unAuthSure.setOnClickListener {
            addLog("确定","进入在线授权页")
            OnlineAuthActivity.start(this,mNoAuthorizeList)
            finish()
        }
    }

    override fun onKeyDownEvent(keyCode: Int, event: KeyEvent?): Boolean {
        mUnauthorizedAdapter.onKeyDown(keyCode, event)
        if(keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_BACK){
            unAuthCancel.performClick()
        }
        if(keyCode == KeyEvent.KEYCODE_F4 || keyCode == KeyEvent.KEYCODE_ENTER){
            unAuthSure.performClick()
        }
        return true
    }


    fun addLog(action: String, remark: String) {
        LogEntity.addLog(
            action,
            "",
            "未授权列表",
            ViewModelQuick.get(this, ShowDelayDataPresenter::class.java).getTotal(),
            0.0,
            0.0,
            "",
            0,
            0,
            "",
            remark
        )
    }
}